package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;
    /**
     * 添加套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    //清除
    @CacheEvict(value = "setmeal",allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("套餐信息:{}",setmealDto);
        setmealService.addSetmeal(setmealDto);
        return R.success("保存套餐成功");
    }

    /**
     * 套餐分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //创建分页构造器
        Page pageinfo = new Page(page,pageSize);
        Page<SetmealDto> dtopage = new Page();

        //条件构造器
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        //添加条件
        qw.like(name!=null,Setmeal::getName,name);
        //添加排序条件  根据修改时间降序排列
        qw.orderByDesc(Setmeal::getUpdateTime);
        //执行分页查询
        setmealService.page(pageinfo,qw);

        //对象拷贝
        BeanUtils.copyProperties(pageinfo,dtopage,"records");
        //取出pageinfo中的records
        List<Setmeal> records = pageinfo.getRecords();
        //遍历集合
        List<SetmealDto> list = records.stream().map((item)->{
            //创建SetmealDto用来存放分类名称
            SetmealDto setmealDto = new SetmealDto();
            //对象拷贝
            BeanUtils.copyProperties(item,setmealDto);
            //获取分类id
            Long categoryId = item.getCategoryId();
            //通过分类id类获取分类对象
            Category category = categoryService.getById(categoryId);
            if(category!=null){
                //通过分类对象获取分类名称
                String categoryName = category.getName();
                //把分类名称放入setmealDto
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());  //

        dtopage.setRecords(list);
        return R.success(dtopage);
    }

    /**
     * 根据id删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    //清除
    @CacheEvict(value = "setmeal",allEntries = true)
    public R<String> deleteSetmeal(@RequestParam List<Long> ids){
        log.info("ids:{}",ids);
        setmealService.removeWithDish(ids);  //
        return R.success("套餐删除成功");
    }

    /**
     * 修改套餐起售停售
     * @param ids
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    //清除
    @CacheEvict(value = "setmeal",allEntries = true)
    public R<String> updateStatus(@RequestParam List<Long> ids,@PathVariable Integer status){
        for (Long id : ids) {
            Setmeal setmeal = new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(status);
            setmealService.updateById(setmeal);
        }
        return R.success("操作成功");
    }

    /**
     * 套餐信息回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> get(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.getByIdWithsetmeal(id);
        return R.success(setmealDto);
    }

    /**
     * 修改菜品信息
     * @param setmealDto
     * @return
     */
    @PutMapping
    //清除
    @CacheEvict(value = "setmeal",allEntries = true)
    public R<String> update(@RequestBody SetmealDto setmealDto){
        log.info(setmealDto.toString());
        setmealService.updateWithSetmeal(setmealDto);
        return R.success("套餐信息修改成功");
    }

    /**
     * 根据条件查询套餐数据
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId + '_' + #setmeal.status")
    public R<List<Setmeal>> list( Setmeal setmeal){

        //条件构造器
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        queryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        //排序
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        //调用方法
        List<Setmeal> list = setmealService.list(queryWrapper);

        return R.success(list);
    }

}
